<?php
namespace app\api\controller;

use think\facade\Db;
use think\facade\Log;
use think\facade\Request;
use think\facade\Cache;

class AcceptPoc
{
    private $redis;

    public function __construct()
    {
        $this->redis = Cache::store('redis')->handler();
    }

    public function index()
    {
        $params = Request::param();
        //记录日志
        $time   = getUtcTime(false);
        Log::write("PocPay_({$time})-----------------------start\n");
        Log::write($params);
        Log::write("--------------\n");

        if(!is_array($params)){
            $params = json_decode($params , true);
        }
        $reference      = $params['reference'] ?? '';
        $transactionNo  = $params['transactionNo'] ?? '';
        $utime          = $params['time'] ?? '';
        if(!empty($utime)){
            $utime = substr($utime,0,4) . '-'
            . substr($utime,4,2) . '-'
            . substr($utime,6,2) . ' '
            . substr($utime,8,2) . ':'
            . substr($utime,10,2) . ':'
            . substr($utime,12,2);
        }
        $status         = $params['status'] ?? '';
        $verifySign     = $params['verifySign'] ?? '';
        $list = Db::name('user_poc_pay')->where([
            'reference'         =>$reference,
            'transactionNo'     =>$transactionNo,
            'is_ok'             => 0
        ])->find();

        if(empty($list)){
            Log::write("Error: not find reference \n");
            Log::write("PocPay_({$time})-------------------------end\n");
            exit;
        }

        $create_time    = getUtcTime();
        $blog = false;
        if($status == 'success'){
            Db::startTrans();
            try{
                $user_id        = $list['user_id'];
                $amount         = $list['amount'];
                $ctime          = $list['ctime'];
                $user_list      = Db::name('user')->find($user_id);
                $user_balance   = $user_list['balance'];
                $result_balance = $user_balance+$amount;
                Db::name('user_cash_log')->save([
                    'user_id'       => $user_id,
                    'type'          => 0, //1=debit
                    'action_type'   => 1, //3=消费
                    'amount'        => $amount,
                    'balance'       => $result_balance, //如果是进行修复的话，这里的余额要重新计算
                    'desc'          => "聚合支付",
                    'ctime'         => strtotime($utime) - 13*60*60
                ]);
                $check_num  = 'P' . getRandNumber();
                Db::name('fund_check')->save([
                    'user_id'       => $user_id,
                    'check_num'     => $check_num,
                    'amount'        => $amount,
                    'balance'       => $result_balance,
                    'ctime'         => strtotime($ctime) - 13*60*60,
                    'utime'         => strtotime($utime) - 13*60*60,
                    'check_status'  => 1,
                    'pay_type'      => 5,   //5 聚合支付
                    'trn'           => $transactionNo,
                    'desc'          => "聚合支付",
                    'msg'        => $utime
                ]);
                Db::name('user')->where(['id'=>$user_id])->update(['balance'=>$result_balance]);
                Db::name('user_poc_pay')->where([
                    'reference'         =>$reference,
                    'transactionNo'     =>$transactionNo,
                ])->update([
                    'verifySign' => $verifySign,
                    'utime'      => $utime,
                    'status'     => $status,
                    'is_ok'      => 1
                ]);
                $blog = true;
                Db::commit();
            } catch (\Exception $e) {
                $error_msg  = $e->getMessage();
                Db::name('api_error')->save([
                    'user_id'       => $user_id,
                    'amount'        => $amount,
                    'amount_bank'   => $amount,
                    'type'          => 'pocket_fund',
                    'data_param'    => $error_msg,
                    'ctime'         => $create_time
                ]);
                Db::rollback();
            }
        }
        if($blog){
            echo 'success';
        }
        Log::write("PocPay_({$time})------{$blog}-------------------end\n");
    }
}
